<?php

class Logo {

    private $_ID;
    private $_path;
    private $_link;
    private $_status;
    private $_order;
    private $_categoryID;
    private $_title;
    private $_createdDate;
    private $_modifiedDate;
    private $_createdUserID;
    private $_modifiedUserID;
    private $_category;
    private $_createdUser;
    private $_modifiedUser;
    private $_enableDate;
    private $_disableDate;
    private $_indefinitely;

    public function getID() {
        return $this->_ID;
    }

    public function getPath() {
        return $this->_path;
    }

    public function getLink() {
        return $this->_link;
    }

    public function getStatus() {
        return $this->_status;
    }

    public function getOrder() {
        return $this->_order;
    }

    public function getCategoryID() {
        return $this->_categoryID;
    }

    public function getTitle() {
        return $this->_title;
    }

    public function getCreatedDate() {
        return $this->_createdDate;
    }

    public function getModifiedDate() {
        return $this->_modifiedDate;
    }

    public function getCreatedUserID() {
        return $this->_createdUserID;
    }

    public function getModifiedUserID() {
        return $this->_modifiedUserID;
    }

    public function getCategory() {
        return $this->_category;
    }

    public function getCreatedUser() {
        return $this->_createdUser;
    }

    public function getModifiedUser() {
        return $this->_modifiedUser;
    }

    public function getEnableDate() {
        return $this->_enableDate;
    }

    public function getDisableDate() {
        return $this->_disableDate;
    }

    public function getIndefinitely() {
        return $this->_indefinitely;
    }

    public function setID($ID) {
        $this->_ID = $ID;
    }

    public function setPath($path) {
        $this->_path = $path;
    }

    public function setLink($link) {
        $this->_link = $link;
    }

    public function setStatus($status) {
        $this->_status = $status;
    }

    public function setOrder($order) {
        $this->_order = $order;
    }

    public function setCategoryID($categoryID) {
        $this->_categoryID = $categoryID;
    }

    public function setTitle($title) {
        $this->_title = $title;
    }

    public function setCreatedDate($createdDate) {
        $this->_createdDate = $createdDate;
    }

    public function setModifiedDate($modifiedDate) {
        $this->_modifiedDate = $modifiedDate;
    }

    public function setCreatedUserID($createdUserID) {
        $this->_createdUserID = $createdUserID;
    }

    public function setModifiedUserID($modifiedUserID) {
        $this->_modifiedUserID = $modifiedUserID;
    }

    public function setCategory($category) {
        $this->_category = $category;
    }

    public function setCreatedUser($createdUser) {
        $this->_createdUser = $createdUser;
    }

    public function setModifiedUser($modifiedUser) {
        $this->_modifiedUser = $modifiedUser;
    }

    public function setEnableDate($enableDate) {
        $this->_enableDate = $enableDate;
    }

    public function setDisableDate($disableDate) {
        $this->_disableDate = $disableDate;
    }

    public function setIndefinitely($indefinitely) {
        $this->_indefinitely = $indefinitely;
    }

    public function add($db = null) {
        if ($db != null) {
            $sql = "INSERT INTO `logo`
                                (
                                 `LG_Path`,
                                 `LG_Link`,
                                 `LG_CreatedDate`,
                                 `LG_Status`,
                                 `LG_Order`,
                                 `LG_CategoryID`,
                                 `LG_Title`,
                                 `LG_ModifiedDate`,
                                 `LG_CreatedUser`,
                                 `LG_ModifiedUser`,
                                 `LG_EnableDate`,
                                 `LG_DisableDate`,
                                 `LG_Indefinitely`)
                    VALUES (
                            :LG_Path,
                            :LG_Link,
                            :LG_CreatedDate,
                            :LG_Status,
                            :LG_Order,
                            :LG_CategoryID,
                            :LG_Title,
                            :LG_ModifiedDate,
                            :LG_CreatedUser,
                            :LG_ModifiedUser,
                            :LG_EnableDate,
                            :LG_DisableDate,
                            :LG_Indefinitely);";

            $parameters = array(
                ':LG_Path', ':LG_Link',
                ':LG_CreatedDate', ':LG_Status',
                ':LG_Order', ':LG_CategoryID',
                ':LG_Title', ':LG_ModifiedDate',
                ':LG_CreatedUser', ':LG_ModifiedUser',
                ':LG_EnableDate', ':LG_DisableDate',
                ':LG_Indefinitely'
            );

            $values = array(
                $this->getPath(), $this->getLink(),
                $this->getCreatedDate(), $this->getStatus(),
                $this->getOrder(), $this->getCategoryID(),
                $this->getTitle(), $this->getModifiedDate(),
                $this->getCreatedUserID(), $this->getModifiedUserID(),
                $this->getEnableDate(), $this->getDisableDate(),
                $this->getIndefinitely()
            );

            $types = array(
                PDO::PARAM_STR, PDO::PARAM_STR,
                PDO::PARAM_STR, PDO::PARAM_INT,
                PDO::PARAM_INT, PDO::PARAM_INT,
                PDO::PARAM_STR, PDO::PARAM_STR,
                PDO::PARAM_INT, PDO::PARAM_INT,
                PDO::PARAM_STR, PDO::PARAM_STR,
                PDO::PARAM_INT
            );
            return $db->executeNonQuery($sql, $parameters, $values, $types);
        }
    }

    public function delete($db = null) {
        if ($db != null) {
            $sql = "DELETE
                    FROM `logo`
                    WHERE `LG_ID` = :LG_ID;";

            $parameters = array(':LG_ID');
            $values = array($this->getID());
            $types = array(PDO::PARAM_INT);
            return $db->executeNonQuery($sql, $parameters, $values, $types);
        }
    }

    public function update($db = null) {

        if ($db != null) {
            $sql = "UPDATE `logo`
                    SET `LG_ID` = :LG_ID,
                      `LG_Path` = :LG_Path,
                      `LG_Link` = :LG_Link,
                      `LG_Status` = :LG_Status,
                      `LG_Order` = :LG_Order,
                      `LG_CategoryID` = :LG_CategoryID,
                      `LG_Title` = :LG_Title,
                      `LG_ModifiedDate` = :LG_ModifiedDate,
                      `LG_ModifiedUser` = :LG_ModifiedUser,
                      `LG_EnableDate` = :LG_EnableDate,
                      `LG_DisableDate` = :LG_DisableDate,
                      `LG_Indefinitely` = :LG_Indefinitely
                    WHERE `LG_ID` = :LG_ID;";


            $parameters = array(
                ':LG_Path', ':LG_Link',
                ':LG_Status',
                ':LG_Order', ':LG_CategoryID',
                ':LG_Title', ':LG_ModifiedDate',
                ':LG_ModifiedUser',
                ':LG_EnableDate', ':LG_DisableDate',
                ':LG_Indefinitely', ':LG_ID'
            );

            $values = array(
                $this->getPath(), $this->getLink(),
                $this->getStatus(),
                $this->getOrder(), $this->getCategoryID(),
                $this->getTitle(), $this->getModifiedDate(),
                $this->getModifiedUserID(),
                $this->getEnableDate(), $this->getDisableDate(),
                $this->getIndefinitely(), $this->getID()
            );

            $types = array(
                PDO::PARAM_STR, PDO::PARAM_STR,
                PDO::PARAM_INT,
                PDO::PARAM_INT, PDO::PARAM_INT,
                PDO::PARAM_STR, PDO::PARAM_STR,
                PDO::PARAM_INT,
                PDO::PARAM_STR, PDO::PARAM_STR,
                PDO::PARAM_INT, PDO::PARAM_INT
            );

            return $db->executeNonQuery($sql, $parameters, $values, $types);
        }
    }

    public function getListByCategoryID($db = null) {
        if ($db != null) {

            $sql = 'SELECT
                    `LG_ID`,
                    `LG_Path`,
                    `LG_Link`,
                    `LG_CreatedDate`,
                    `LG_Status`,
                    `LG_Order`,
                    `LG_CategoryID`,
                    `LG_Title`,
                    `LG_ModifiedDate`,
                    `LG_CreatedUser`,
                    `LG_ModifiedUser`,
                    `LG_EnableDate`,
                    `LG_DisableDate`,
                    `LG_Indefinitely`
                  FROM `logo`
                  WHERE LG_CategoryID = :LG_CategoryID
                  ORDER BY LG_Order DESC'
            ;

            $parameters = array(':LG_CategoryID');
            $values = array($this->getCategoryID());
            $types = array(PDO::PARAM_INT);

            $result = $db->executeQuery($sql, $parameters, $values, $types);

            if ($result != null) {
                $rows = array();

                while ($row = $result->fetch()) {

                    $logo = new Logo();
                    $logo->setID($row->LG_ID);
                    $logo->setPath($row->LG_Path);
                    $logo->setLink($row->LG_Link);
                    $logo->setCreatedDate($row->LG_CreatedDate);
                    $logo->setStatus($row->LG_Status);
                    $logo->setOrder($row->LG_Order);
                    $logo->setCategoryID($row->LG_CategoryID);
                    $logo->setTitle($row->LG_Title);
                    $logo->setModifiedDate($row->LG_ModifiedDate);
                    $logo->setCreatedUserID($row->LG_CreatedUser);
                    $logo->setModifiedUserID($row->LG_ModifiedUser);
                    $logo->setEnableDate($row->LG_EnableDate);
                    $logo->setDisableDate($row->LG_DisableDate);
                    $logo->setIndefinitely($row->LG_Indefinitely);

                    $createdUser = new Users();
                    $createdUser->setID($logo->getCreatedUserID());
                    $createdUser->getByID($db);
                    $logo->setCreatedUser($createdUser);


                    $modifiedUser = new Users();
                    $modifiedUser->setID($logo->getModifiedUserID());
                    $modifiedUser->getByID($db);
                    $logo->setModifiedUser($modifiedUser);

                    $category = new Categories();
                    $category->setID($logo->getCategoryID());
                    $category->getByID($db);
                    $logo->setCategory($category);

                    $rows[] = $logo;
                }
                return $rows;
            } else {
                return null;
            }
        }
    }

    public function getByID($db = null) {
        if ($db != null) {
            $sql = 'SELECT
                    `LG_ID`,
                    `LG_Path`,
                    `LG_Link`,
                    `LG_CreatedDate`,
                    `LG_Status`,
                    `LG_Order`,
                    `LG_CategoryID`,
                    `LG_Title`,
                    `LG_ModifiedDate`,
                    `LG_CreatedUser`,
                    `LG_ModifiedUser`,
                    `LG_EnableDate`,
                    `LG_DisableDate`,
                    `LG_Indefinitely`
                  FROM `logo`
                  WHERE LG_ID = :LG_ID';
            $parameters = array(':LG_ID');
            $values = array($this->getID());
            $types = array(PDO::PARAM_INT);

            $result = $db->executeQuery($sql, $parameters, $values, $types);

            // nếu có kết quả
            if ($result != null) {

                $row = $result->fetch();

                $this->setID($row->LG_ID);
                $this->setPath($row->LG_Path);
                $this->setLink($row->LG_Link);
                $this->setCreatedDate($row->LG_CreatedDate);
                $this->setStatus($row->LG_Status);
                $this->setOrder($row->LG_Order);
                $this->setCategoryID($row->LG_CategoryID);

                $this->setTitle($row->LG_Title);

                $this->setModifiedDate($row->LG_ModifiedDate);
                $this->setCreatedUserID($row->LG_CreatedUser);
                $this->setModifiedUserID($row->LG_ModifiedUser);
                $this->setEnableDate($row->LG_EnableDate);
                $this->setDisableDate($row->LG_DisableDate);
                $this->setIndefinitely($row->LG_Indefinitely);

                $createdUser = new Users();
                $createdUser->setID($this->getCreatedUserID());
                $createdUser->getByID($db);
                $this->setCreatedUser($createdUser);


                $modifiedUser = new Users();
                $modifiedUser->setID($this->getModifiedUserID());
                $modifiedUser->getByID($db);
                $this->setModifiedUser($modifiedUser);

                $category = new Categories();
                $category->setID($this->getCategoryID());
                $category->getByID($db);
                $this->setCategory($category);

                return $this;
            } else {
                return null;
            }
        }
    }

    public function getMaxOrder($db) {
        if ($db != null) {
            $sql = "SELECT MAX(LG_Order) + 1 AS MaxOrder FROM logo";
            $parameters = null;
            $values = null;
            $types = null;
            $result = $db->executeQuery($sql, $parameters, $values, $types);

            $row = $result->fetch();
            if ($row->MaxOrder != null) {
                return $row->MaxOrder;
            } else {
                return 1;
            }
        }
    }

    public function updateStatus($db = null) {
        if ($db != null) {
            $sql = "UPDATE `logo`
                    SET `LG_Status` = :LG_Status
                    WHERE `LG_ID` = :LG_ID;";
            $parameters = array(':LG_Status', ':LG_ID');
            $values = array($this->getStatus(), $this->getID());
            $types = array(PDO::PARAM_INT, PDO::PARAM_INT);
            return $db->executeNonQuery($sql, $parameters, $values, $types);
        }
    }

    public function orderUp($db) {
        if ($db != null) {
            // câu lệnh tải currentOrder, nextOrder, nextArticleID
            $sql = "SELECT (SELECT `LG_Order` 
                    from logo 
                    WHERE LG_ID = :LG_ID) As `currenOrder`,               
                
                    (SELECT `LG_Order` from logo WHERE `LG_Order` > 
                    (SELECT `LG_Order` from logo WHERE LG_ID = :LG_ID) 
                    Order by `LG_Order` LIMIT 1) As `nextOrder`,

                    (SELECT LG_ID from logo WHERE `LG_Order` = 
                    (SELECT `LG_Order` from logo WHERE `LG_Order` > 
                    (SELECT `LG_Order` from logo WHERE LG_ID = :LG_ID) 
                    Order by `LG_Order` LIMIT 1)) As `nextID`";

            $parameters = array(':LG_ID');
            $values = array($this->getID());
            $types = array(PDO::PARAM_INT);
            $result = $db->executeQuery($sql, $parameters, $values, $types);

            if ($result != null) {
                $row = $result->fetch();

                if ($row->nextID != null && $row->nextOrder != null) {
                    // cập nhật order của previous article bằng với currentOrder
                    $sqlNextArticle = "update logo set `LG_Order` = :LG_Order where `LG_ID` = :LG_ID";
                    $parameters2 = array(':LG_Order', ':LG_ID');
                    $values2 = array($row->currenOrder, $row->nextID);
                    $types2 = array(PDO::PARAM_INT, PDO::PARAM_INT);
                    $db->executeNonQuery($sqlNextArticle, $parameters2, $values2, $types2);

                    // cập nhật order của current article bằng với previous order
                    $sqlCurrentArticle = "update logo set `LG_Order` = :LG_Order where `LG_ID` = :LG_ID";
                    $parameters3 = array(':LG_Order', ':LG_ID');
                    $values3 = array($row->nextOrder, $this->getID());
                    $types3 = array(PDO::PARAM_INT, PDO::PARAM_INT);
                    $db->executeNonQuery($sqlCurrentArticle, $parameters3, $values3, $types3);
                }
            }
        }
    }

    function orderDown($db) {
        if ($db != null) {
            // câu lệnh tải currentOrder, previousOrder, previousArticleID
            $sql = "SELECT (SELECT `LG_Order` from logo 
                WHERE LG_ID = :LG_ID) As `currenOrder`, 
                    
		(SELECT `LG_Order` from logo WHERE `LG_Order` < 
		(SELECT `LG_Order` from logo WHERE LG_ID = :LG_ID) 
		Order by `LG_Order` desc LIMIT 1) As `previousOrder`,
                
		(SELECT LG_ID from logo WHERE `LG_Order` = 
		(SELECT `LG_Order` from logo WHERE `LG_Order` < 
		(SELECT `LG_Order` from logo WHERE LG_ID = :LG_ID) 
		Order by `LG_Order` desc LIMIT 1)) As `previousID`";

            $parameters = array(':LG_ID');
            $values = array($this->getID());
            $types = array(PDO::PARAM_INT);

            $result = $db->executeQuery($sql, $parameters, $values, $types);

            if ($result != null) {

                $row = $result->fetch();

                if ($row->previousID != null && $row->previousOrder != null) {
                    // cập nhật order của previous bằng với currentOrder
                    $sqlPreviousArticle = "update logo set `LG_Order` = :LG_Order where `LG_ID` = :LG_ID";
                    $parameters2 = array(':LG_Order', ':LG_ID');
                    $values2 = array($row->currenOrder, $row->previousID);
                    $types2 = array(PDO::PARAM_INT, PDO::PARAM_INT);
                    $db->executeNonQuery($sqlPreviousArticle, $parameters2, $values2, $types2);

                    // cập nhật order của current article bằng với previous order
                    $sqlCurrentArticle = "update logo set `LG_Order` = :LG_Order where `LG_ID` = :LG_ID";
                    $parameters3 = array(':LG_Order', ':LG_ID');
                    $values3 = array($row->previousOrder, $this->getID());
                    $types3 = array(PDO::PARAM_INT, PDO::PARAM_INT);
                    $db->executeNonQuery($sqlCurrentArticle, $parameters3, $values3, $types3);
                }
            }
        }
    }

}

?>
